package org.szgb.console.sys.line.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.line.entity.SysLineLocation;
import org.szgb.console.sys.line.mapper.SysLineLocationMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysLineLocationService {
    private final SysLineLocationMapper sysLineLocationMapper;

    @Autowired
    public SysLineLocationService(SysLineLocationMapper sysLineLocationMapper) {
        this.sysLineLocationMapper = sysLineLocationMapper;
    }

    @Transactional(readOnly = true)
    public SysLineLocation get(String id) {
        return sysLineLocationMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysLineLocation> findList(SysLineLocation sysLineLocation) {
        if (sysLineLocation != null && sysLineLocation.getPageDto() != null) {
            Page<SysLineLocation> page = new Page<>(sysLineLocation.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysLineLocation.setPage(page);
        }
        return sysLineLocationMapper.findList(sysLineLocation);
    }

    @Transactional(readOnly = true)
    public List<String> getNameListByIdList(String[] ids) {
        return sysLineLocationMapper.getNameListByIdList(ids);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysLineLocation sysLineLocation) {
        return sysLineLocationMapper.getCount(sysLineLocation);
    }

    @Transactional()
    public void save(SysLineLocation sysLineLocation) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysLineLocation.getId() == null || sysLineLocation.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysLineLocation.setCreatedBy(sysLineLocation.getUpdatedBy());
            // 添加UUID
            sysLineLocation.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysLineLocationMapper.insert(sysLineLocation);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysLineLocationMapper.update(sysLineLocation);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysLineLocationMapper.delete(ids.split(","));
    }
}
